package com.example.padlock.view.activity;


import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.padlock.R;
import com.example.padlock.view.sendvedio.ConnectThread;
import com.example.padlock.view.sendvedio.ConnectThread.OnSendListener;
import com.example.padlock.view.sendvedio.VedioSender;

public class SendVedioActivity extends Activity implements OnSendListener,OnClickListener{

	SurfaceView msur;
	private TextView tv;
	Handler handler;
	private OutputStream mOS;
	ConnectThread thread;
	private VedioSender vedioSender;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sendvedio);
        
        handler = new Handler(){
        	@Override
        	public void handleMessage(Message msg) {
        		// TODO Auto-generated method stub
        		super.handleMessage(msg);
        		switch(msg.what){
        		case 1:
        			mToastMsg(msg.getData().getString("myMsg"));
        			break;
        		case 2:
        			break;
        		default:
        			break;
        		}
        	}
        };
        msur = (SurfaceView) findViewById(R.id.surfaceView1);
        thread=new ConnectThread("115.29.176.161", 31500);
        thread.setOnSendListener(this);
        vedioSender = new VedioSender(msur,thread);
        thread.start();
        tv=(TextView)findViewById(R.id.textView1);
        tv.setOnClickListener(this);
        
        
//        new Thread(){
//        	public void run() {
////        		sendMsg("start Connect...");
//        		vedioSender.send("115.29.176.161", 31500);
//////        		sendMsg("DisConnect!");
//        	};
//        	
//        	public void sendMsg(String str){
//        		Bundle bundle = new Bundle();
//        		bundle.putString("myMsg", str);
//        		Message msg = new Message().obtain();
//        		msg.what = 1;
//        		msg.setData(bundle);
//        		handler.sendMessage(msg);
//        	}
//        	
//        }.start();
        
        
    }
    
    void mToastMsg(String str){
    	Toast.makeText(SendVedioActivity.this, str, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    }
    

	@Override
	public void onSend(OutputStream os) {
		System.out.println("call:"+"calling");
		final VedioSender send=vedioSender;
		send.isSend=true;
		final OutputStream o=os;
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("send:"+"data");
				send.send(o);
			}
		});
	}

	@Override
	public void onClick(View v) {
		finish();
	}
}
